﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YahooFinanceApi;

namespace Entrance
{
    class Program
    {
        static void Main(string[] args)
        {
            if (!Directory.Exists("Result"))
            {
                Directory.CreateDirectory("Result");
            }
            var stockList = File.ReadAllLines("StockList.txt").ToList();
            var startTime = new DateTime(2015, 4, 13);
            var dateTime = new DateTime(2020, 4, 14); 
            foreach (var symbol in stockList)
            {
                try
                {
                    var data = GetStockData(symbol, startTime, dateTime);
                    Console.WriteLine(symbol+":Finished");
                    File.WriteAllText($"Result/{symbol}.csv", data);
                 
                }
                catch (Exception e)
                {
                    Console.WriteLine(symbol+":Filed");
                } 
            }
        }

        private static string GetStockData(string symbol, DateTime startTime, DateTime dateTime)
        {
            var candles = Yahoo.GetHistoricalAsync(symbol, startTime, dateTime, Period.Weekly);
            candles.Wait();
            var r = candles.Result;
            string data = "";
            data += ("DateTime,Open,High,Low,Close,Volume,Adjusted\r\n");
            
            foreach (var candle in r)
            {
                data +=
                    ($"{candle.DateTime},{candle.Open},{candle.High},{candle.Low},{candle.Close},{candle.Volume},{candle.AdjustedClose}\r\n"
                    );
            }

            return data;
        }

        public class LinearHelper
        {
            public static double[] LinearResult(double[] arrayX, double[] arrayY)
            {
                double[] result = { 0, 0 };

                if (arrayX.Length == arrayY.Length)
                {
                    double averX = arrayX.Average();
                    double averY = arrayY.Average();
                    result[0] = Scale(averX, averY, arrayX, arrayY);
                    result[1] = Offset(result[0], averX, averY);
                }
                return result;
            }

            private static double Scale(double averX, double averY, double[] arrayX, double[] arrayY)
            {
                double scale = 0;
                if (arrayX.Length == arrayY.Length)
                {
                    double Molecular = 0;
                    double Denominator = 0;
                    for (int i = 0; i < arrayX.Length; i++)
                    {
                        Molecular += (arrayX[i] - averX) * (arrayY[i] - averY);
                        Denominator += Math.Pow((arrayX[i] - averX), 2);
                    }
                    scale = Molecular / Denominator;
                }

                return scale;
            }

            private static double Offset(double scale, double averX, double averY)
            {
                double offset = 0;
                offset = averY - scale * averX;
                return offset;
            }
        }
    }
}
